//
//  QuestionTabViewCell.m
//  PCAForWar
//
//  Created by dllo on 16/3/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "QuestionTabViewCell.h"
#import "ModelQuestion.h"
#import <UIImageView+WebCache.h>

#define  WIDTH [UIScreen mainScreen].bounds.size.width

@implementation QuestionTabViewCell

- (void)dealloc{
    
    [_labelForAnswerContect release];
    [_labelForAnswerName release];
    [_imageForAnswer release];
    [_labelForContect release];
    [_labelForAskName release];
    [_imageForHeadPicture release];
    [_viewFortemp release];
    [_labelForTime release];
    
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        [self createView];
        [self createheadViewPic];
        [self createLabel];
    }
    
    
    return self;
}

-(void)passModelWith:(ModelQuestion *)model {
    
    self.labelForAskName.text = model.userName;
    self.labelForAskName.textColor = [UIColor grayColor];
    
    [self.imageForHeadPicture sd_setImageWithURL:[NSURL URLWithString:model.userHeadPicUrl] placeholderImage:[UIImage imageNamed:@"jiazai"]];
    
    //contect
    self.labelForContect.text = model.content[0];
    
    [self.imageForAnswer sd_setImageWithURL:[NSURL URLWithString:model.specialistHeadPicUrl]];
    
    self.labelForAnswerName.text = model.specialistName;
    self.labelForAnswerContect.text = model.content[1];
    
    //回复时间
    //时间
    
    CGFloat modelTime = model.cTime.integerValue;
    
    modelTime = modelTime / 1000;
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm"];
    [dateFormatter setDateFormat:@"MM-dd"];
    [dateFormatter release];
    
    NSDate *now = [NSDate date];
    NSTimeInterval time = [now timeIntervalSince1970];
    float seconds = (int)time - modelTime;
    float points = seconds / 60;
    float hours = points / 60;
    
    if (points < 60) {
        
        self.labelForTime.text = [NSString stringWithFormat:@"%d分钟前", (int)points];
        
    }else if (points > 60 && points < 3600) {
        
        self.labelForTime.text = [NSString stringWithFormat:@"1小时前"];
        
    }else if (points > 3600 && points< 7200) {
        
        self.labelForTime.text = [NSString stringWithFormat:@"2小时前"];
        
    }else if (points > 7200 && points< 10800) {
        
        self.labelForTime.text = [NSString stringWithFormat:@"3小时前"];
        
    }else if (points > 10800 && points< 14400) {
        
        self.labelForTime.text = [NSString stringWithFormat:@"4小时前"];
        
    }else if (points > 14400 && points< 18000) {
        
        self.labelForTime.text = [NSString stringWithFormat:@"5小时前"];
        
    }else if (points > 18000 && points< 21600) {
        
    }else if (hours > 24 && hours < 48) {
        
        self.labelForTime.text = [NSString stringWithFormat:@"一天前"];
        
    }else {
        
        self.labelForTime.text = [NSString stringWithFormat:@"二天前"];
        
    }
    
}

//边框view
- (void)createView {
    
    self.viewFortemp = [[UIView alloc] init];
    [self.contentView addSubview:self.viewFortemp];
    self.viewFortemp.layer.masksToBounds = YES;
    self.viewFortemp.layer.cornerRadius = 5;
    self.viewFortemp.layer.borderWidth = 1;
    self.viewFortemp.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    [_viewFortemp release];
}

- (void)createLabel {
    
    self.labelForAskName = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelForAskName];
    
    self.labelForContect = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelForContect];
    self.labelForContect.numberOfLines = 0;
    self.labelForContect.textColor = [UIColor grayColor];
    
    self.labelForAnswerName = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelForAnswerName];
    
    
    self.labelForAnswerContect = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelForAnswerContect];
    self.labelForAnswerContect.numberOfLines = 0;
    
    
    self.labelForTime = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelForTime];
    
    [_labelForAskName release];
    [_labelForContect release];
    [_labelForAnswerName release];
    [_labelForAnswerContect release];
    
}

- (void)createheadViewPic {
    
    //头像
    self.imageForHeadPicture = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"jiazai"]];
    [self.contentView addSubview:self.imageForHeadPicture];
    [self.imageForHeadPicture release];
    self.imageForHeadPicture.layer.masksToBounds = YES;
    self.imageForHeadPicture.layer.cornerRadius = 20;
    
    self.imageForAnswer = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"jiazai"]];
    [self.contentView addSubview:self.imageForAnswer];
    [self.imageForAnswer release];
    self.imageForAnswer.layer.masksToBounds = YES;
    self.imageForAnswer.layer.cornerRadius = 20;
    
    [_imageForHeadPicture release];
    [_imageForAnswer release];
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    self.viewFortemp.frame = CGRectMake(10, 10, self.frame.size.width - 20, self.frame.size.height - 20);
    //头像尺寸
    self.imageForHeadPicture.frame = CGRectMake(22, 30, 40, 40);
    self.labelForAskName.frame = CGRectMake(70, 35, self.frame.size.width - 80, 30);
    self.labelForContect.frame = CGRectMake(70, 70, self.frame.size.width - 100, [[self class] heightForLabel:self.labelForContect.text]);
    
    CGFloat h = [[self class] heightForLabel:self.labelForContect.text];
    self.imageForAnswer.frame = CGRectMake(20, h + 60 + 100, 40, 40);
    
    self.labelForAnswerName.frame = CGRectMake(70, h + 60 + 100, 70, 30);
    self.labelForAnswerContect.frame = CGRectMake(70, h+ 60 + 100 + 30, self.frame.size.width - 100, [[self class] heightForLabel:self.labelForAnswerContect.text]);
    
    CGFloat z = h + 190 + [[self class] heightForLabel:self.labelForAnswerContect.text];
    self.labelForTime.frame = CGRectMake(self.frame.size.width - 90, z + 10, 80, 40);
    self.labelForTime.font = [UIFont systemFontOfSize:14];
    self.labelForTime.textColor = [UIColor grayColor];
    
    self.g = z + 40 +20;
    
}

+(CGFloat)heightForLabel:(NSString *)text{
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake(WIDTH - 100, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    
    return rect.size.height;
}

+ (CGFloat)heightForCell:(ModelQuestion *)model {
    
    CGFloat h = [QuestionTabViewCell heightForLabel:model.content[0]] + [QuestionTabViewCell heightForLabel:model.content[1]]+ 190 + 60;
    
    return h;
    
}






















- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
